Repository navigation
Conversation
|
Seems like a reasonable refactoring; I don't know enough about Rust to know what would be the best design, but the way that the C++ library developed made having a generic data container for arrays (i.e. ArrayData) desirable architecturally. |
|
Thanks @wesm . I think this PR is mostly ready for review - I still need to fix the Windows test failure. @andygrove @pitrou @crepererum @xhochy : it would be great if you can take a look too. Thanks! |
pitrou
left a comment
There was a problem hiding this comment.
Thanks for doing this! I don't know much about Rust, but I left a few comments.
There was a problem hiding this comment.
Perhaps say "kinds" instead of "types" if you want to avoid ambiguity about the Rust type of buffers.
There was a problem hiding this comment.
I would say something like "Only non-empty for nested types, currently List and Struct".
There was a problem hiding this comment.
For the record, in the C++ implementation we call count_set_bits lazily (when a caller first asks for the null count). YMMV.
There was a problem hiding this comment.
Yes I thought about that but moving this to null_count() will require it to take a &mut self instead of &self (since now it needs to modify self.null_count), which is not very desirable.
There was a problem hiding this comment.
Apparently Rust allows interior mutability, which is adequate for such cached properties:
https://ricardomartins.cc/2016/06/08/interior-mutability
There was a problem hiding this comment.
Yes, I forgot about Cell. Will change.
There was a problem hiding this comment.
Actually @pitrou : the Cell is not thread-safe in this case so we cannot use it. I thought about using AtomicI64 but it is nightly-only. I'm not sure how significant this is for performance reason - can we leave it as a TODO for now?
There was a problem hiding this comment.
I'm curious, why is this a copy? Data types are mutable?
There was a problem hiding this comment.
Data types are not mutable. The other option is to return a reference here. However, if you need to store this reference anywhere in a struct, you'd have to add a lifetime parameter in that struct, which will get populated to any struct that use this struct, and so on. Personally I'd like to avoid this since it makes the design much more restrictive.
I think it's a good idea to add another version of this method that returns a reference though, in case anyone only needs to use the result in the local stack. Will do that.
There was a problem hiding this comment.
(IMHO!) In Rust, the user should decide if the clone is required or not. In many cases, you could read out information of the data type w/o cloning the entire type. It would follow the "zero-cost abstraction"-principle. And for buffers and child_data you do the same, so it would be consistent. Also, data_type_ref is not really "cheap/efficient by default".
There was a problem hiding this comment.
Got you point. I think we can remove the data_type and rename data_type_ref to data_type. This keep it consistent with others like buffer and child_data. What do you think?
There was a problem hiding this comment.
You could assert more things, for example that offsets[0] == 0 and offsets[array length + 1] == len(values array).
There was a problem hiding this comment.
As a side note, I suppose you'll want to add a BinaryBuilder at some point?
There was a problem hiding this comment.
Yes, will add that in a follow-up PR.
There was a problem hiding this comment.
You'll probably want a variant of this method that also takes a vector of field names.
There was a problem hiding this comment.
This is supposed to only used for testing, and I really want to add more tests when the ArrayBuilder is implemented. For now, let me change this to accept Vec<(Field,ArrayRef)>.
There was a problem hiding this comment.
Why is nullable false? In Arrow you can have individual null elements in a non-null struct value...
There was a problem hiding this comment.
Just some dummy value for testing. I'll change it so that the nullable is passed in.
There was a problem hiding this comment.
Why &v here while you're using plain v in the other for loop below?
There was a problem hiding this comment.
v means to consume the unique reference. We cannot consume the reference twice so the first use needs to be &v - just read it. We can change the second one to &v as well and it should have no difference.
|
I'll have a look tomorrow. Thanks for the PR. |
crepererum
left a comment
There was a problem hiding this comment.
Sorry for the late reply.
First of all, thank you very much for this PR. Great work! Could you please have a look at my comments, esp. regarding security-related questions?
There was a problem hiding this comment.
but this would require the *const u8 is aligned correctly. I think we should check this here. Otherwise, you end up with undefined behavior (or on x86, at least with a SIGBUS).
There was a problem hiding this comment.
(IMHO!) In Rust, the user should decide if the clone is required or not. In many cases, you could read out information of the data type w/o cloning the entire type. It would follow the "zero-cost abstraction"-principle. And for buffers and child_data you do the same, so it would be consistent. Also, data_type_ref is not really "cheap/efficient by default".
There was a problem hiding this comment.
Please make this a private function, I think most users are already OK with https://doc.rust-lang.org/std/primitive.u32.html#method.next_power_of_two
There was a problem hiding this comment.
I feel like you're implementing quite a solid amount of bitfield/-vector stuff here. There are loads of crates that do this and I think we should use them, for the following reason: your implementation might be very slow, if LLVM doesn't remove your division and modulo ops. And there might be other things (edge cases, optimizations) that I think we don't need to care if you just don't invent our own bitfields.
There was a problem hiding this comment.
Yes we can do that, but here we are just doing very simple stuff like get_bit, set_bit, so I don't see a strong need to depend on another crate just for now.
There was a problem hiding this comment.
a) Rust uses len for container sizes, so I think that's what we should call it.
b) why is the internal datatype i64 and you cast to usize here? Let's say we don't support 16bit systems and length hopefully does not contain negative values (which you don't check in ArrayDataBuilder::len, on both 32 and 64 bit this conversion can never fail). So why not storing usize in the first place?
There was a problem hiding this comment.
Good point. I think we should use len and don't return usize here.
I followed the C++ ArrayData structure and used i64 here, but it seems Arrow format specify that Array length should be stored as a 32-bit signed integer. @wesm : could you clarify why int64_t is used for C++ ArrayData length - seems int32_t is enough?
Because of the format specification, we can't use usize to store the length. Also, to my understanding Array is a logical concept and could contain one or more physical Buffers, therefore the length could go beyond the limit of usize(e.g., you can have a 32-bit long Array on a 16-bit platform).
There was a problem hiding this comment.
so now you have allocated a vector with undefined data it it, is that really what you want? I would rather prefer to have an explicit state here (like all-zero).
There was a problem hiding this comment.
uh, that may overflow in production builds (overflows in Rust are only checked in debug builds) and lead to a security issue some lines below, because you read from a out-of-bounds position. Please add some checks here that this doesn't happen:
- check that the integer addition doesn't overflow ( https://doc.rust-lang.org/std/primitive.usize.html#method.checked_add )
- check that the initial
iis actually in-range.
There was a problem hiding this comment.
not sure if this pointer here is actually aligned and points to a valid location?
There was a problem hiding this comment.
It's similar to the last alignment comment. It will be good if we can add some alignment check but I don't know what to use in Rust. Suggestion will be appreciated.
|
@sunchao Sorry I'm a bit late to the party here but I'm working on getting up to speed and have set some time aside tomorrow to pull your branch and understand the refactor. |
andygrove
left a comment
There was a problem hiding this comment.
I ran out of time to really give this a proper review today. It looks good overall and I've made a couple minor comments. I'll try and continue with the review during this week.
There was a problem hiding this comment.
I think this needs to compare self.len to other.len first before calling memcmp?
There was a problem hiding this comment.
should this method be marked unsafe?
|
Thanks for the great comments @pitrou @crepererum and @andygrove ! I'll try to address them soon.
No worries @andygrove - really appreciate it. |
Codecov Report
@@ Coverage Diff @@
## master #2330 +/- ##
==========================================
+ Coverage 84.73% 87.23% +2.49%
==========================================
Files 293 13 -280
Lines 45471 1347 -44124
==========================================
- Hits 38529 1175 -37354
+ Misses 6905 172 -6733
+ Partials 37 0 -37
Continue to review full report at Codecov.
|
|
@sunchao regarding alignment: for C++, I've put quite some effort in the ability to read arrow from partly untrusted sources (using flatbuffer checks and custom runtime checks). Also, I think the check is rather cheap and may improve debugging experience and UX. Regarding "what to use": honestly I thought a simple |
|
Thanks @crepererum ! will add the function and update the code. |
| assert!(data.buffers().len() == 1); | ||
| let values = data.buffers()[0].copy(); | ||
| let raw_values = data.buffers()[0].raw_data(); | ||
| debug_assert!(memory::is_aligned::<u8>(raw_values, mem::size_of::<T>())); |
There was a problem hiding this comment.
why is this an debug assert only? I also would like to protect my production code against these kind of errors.
Also, could you test this? There's should_panic.
There was a problem hiding this comment.
Very good point. I'll change this to assert. For testing though, I need to find a way to bypass allocate_aligned and inject some mis-aligned data for Buffer. Let me think about it...
| let values = make_array(data.child_data()[0].clone()); | ||
| let value_offsets = data.buffers()[0].raw_data() as *const i32; | ||
| let raw_value_offsets = data.buffers()[0].raw_data(); | ||
| debug_assert!(memory::is_aligned(raw_value_offsets, mem::size_of::<i32>())); |
| assert!(data.buffers().len() == 2); | ||
| let value_offsets = data.buffers()[0].raw_data() as *const u8 as *const i32; | ||
| let raw_value_offsets = data.buffers()[0].raw_data(); | ||
| debug_assert!(memory::is_aligned(raw_value_offsets, mem::size_of::<i32>())); |
| } | ||
|
|
||
| /// Check if the pointer `p` is aligned to offset `a`. | ||
| pub fn is_aligned<T>(p: *const T, a: usize) -> bool { |
There was a problem hiding this comment.
I know this is from libcore, but could we have some rough tests for it?
There was a problem hiding this comment.
I think it's not required anymore because of the change in RawPtrBox. Beforehand, it was there because the compiler requires you to use all type parameters (otherwise, rust would have some soundness problems). See https://play.rust-lang.org/?gist=32d1891decd3e0dfcdf14f92ca123e63&version=stable&mode=debug&edition=2015 for what would have happened w/o the marker.
| .add_buffer(Buffer::from(v)) | ||
| .build(); | ||
| let _: PrimitiveArray<i32> = PrimitiveArray::from(Rc::new(arr_data)); | ||
| criterion::black_box(PrimitiveArray::<i32>::from(arr_data)); |
There was a problem hiding this comment.
Just as a side note:
It's completely fine to use this to compare one change to another (like "did this PR result in any regression"), but it may not work to compare the rust against the C++ implementation, because the black box is not probably implemented under stable rust (see criterion source code and the reason why black_box is unstable).
There was a problem hiding this comment.
Thanks @crepererum . Interesting to know more about black_box. I think the purpose for this benchmark is exactly the former case: check whether there's any perf regression.
sunchao
left a comment
There was a problem hiding this comment.
Thanks @crepererum . Really appreciate these great comments!
There was a problem hiding this comment.
Actually @pitrou : the Cell is not thread-safe in this case so we cannot use it. I thought about using AtomicI64 but it is nightly-only. I'm not sure how significant this is for performance reason - can we leave it as a TODO for now?
| assert!(data.buffers().len() == 1); | ||
| let values = data.buffers()[0].copy(); | ||
| let raw_values = data.buffers()[0].raw_data(); | ||
| debug_assert!(memory::is_aligned::<u8>(raw_values, mem::size_of::<T>())); |
There was a problem hiding this comment.
Very good point. I'll change this to assert. For testing though, I need to find a way to bypass allocate_aligned and inject some mis-aligned data for Buffer. Let me think about it...
There was a problem hiding this comment.
Yes PhantomData is no longer needed. Done.
| } | ||
|
|
||
| /// Check if the pointer `p` is aligned to offset `a`. | ||
| pub fn is_aligned<T>(p: *const T, a: usize) -> bool { |
|
Sorry for dragging this for so long. I've addressed most of the comments. |
crepererum
left a comment
There was a problem hiding this comment.
One last tiny request.
| fn test_from_i32() { | ||
| let a = PrimitiveArray::from(vec![15, 14, 13, 12, 11]); | ||
| assert_eq!(5, a.len()); | ||
| #[should_panic(expected = "")] |
There was a problem hiding this comment.
Just picking one should_panic here, but this applies to all of them:
assert! can also take a message, see https://doc.rust-lang.org/std/macro.assert.html , esp. the formatting example at the very end. BUT: since technically this wasn't solved before your PR, let's just agree on creating a ticket (like "rust asserts should all provide messages"), so we can get THIS PR merged.
There was a problem hiding this comment.
Yes, good point. I'll file a separate JIRA for this later.
| let mut v: Vec<u8> = Vec::with_capacity(n); | ||
| for i in 0..n { | ||
| v.push(i as i32); | ||
| v.push((i % 0xffff) as u8); |
There was a problem hiding this comment.
I think this should be i & 0xfff. Yes, technically, this is the same for positive integers (which is ensured since n: usize and therefore i: usize), but modulo operator is a) usually slower (might be implemented using DIV) and b) cannot be optimized that well.
See https://godbolt.org/g/bL2jJE (debug build)
and https://godbolt.org/g/bQi1Zs (release build)
There was a problem hiding this comment.
Ah, interesting to know! Will change. I think you mean i & 0xffff right?
There was a problem hiding this comment.
Yes, I've meant i & 0xffff. Sorry.
|
@andygrove you cool to merge this? |
|
@wesm Yes, I think so. I need to read Uwe's instructions again on how to merge as this will be my first one as a committer. I will try and get to this tomorrow, but Saturday at the latest. |
|
Ok, I will go ahead and merge, there will be other PRs to merge |
|
I just went through all the setup and I'm ready now to merge future PRs. |
This changes the existing
Bufferclass to be non-generic over typeT, since aBufferclass should just represent a plain byte array and interpretation of the data within the buffer should be done on a higher level, such as inArray.While working on this, I found that I also need to make significant changes on the
ArrayandListtypes, since they are currently heavily tied with theBuffer<T>implementation. The new implementation follows arrow-cpp and defines aArrayDatastruct which provides the common operations on a Arrow array. Subtypes ofArraythen provide specific operations for the types they represent. For instance, one can get a primitive value at indexiforPrimitiveArraytype, or can get a column at indexiforStructArray.I removed
Listsince it's no longer necessary. RemovedPrimitiveArray::{min,max}for now but plan to add them back.